//
//  FMDBManager.swift
//  sinaWeibo
//
//  Created by Beans on 16/7/5.
//  Copyright © 2016年 iceWorks. All rights reserved.
//

import Foundation
import FMDB

// 不继承自任何类
class HBDBManager {
    
    // 数据库的操作可以创建一个单例类
    // 1. 方便操作
    // 2. 节省资源
    static let sharedInstance: HBDBManager = HBDBManager()

    // 声明一个FMDatabaseQueue, 便于外界调用
    let queue: FMDatabaseQueue

    // 因没有继承自任何类, 所以不用写override
    private init() {
        // 1. 拼接数据库路径
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("hbweibo.db")
        // 2. 初始化FMDatabaseQueue
        queue = FMDatabaseQueue(path: path)

        // 3. 创建数据表
        createTable()
    }

    // 创建数据表
    private func createTable() {

        let sql = "CREATE TABLE IF NOT EXISTS 'T_Status' (" +
            "'status' text NOT NULL," +
            "'statusId' integer NOT NULL," +
            "'userId' integer NOT NULL," +
            "PRIMARY KEY('statusId')" +
        ");"

        printLog(sql)

        queue.inDatabase { (db: FMDatabase!) -> Void in
            db.executeUpdate(sql, withArgumentsInArray: nil)
        }
    }

}
